from django.db import models

# Create your models here.
from apps.articles.models import Article
from apps.users.models import User, Teacher
from utils.models import BaseModel
from utils.constants import RATING_CHOICES


class Comment(BaseModel):
    """评论"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='评论用户', related_name='comments')
    content = models.TextField(verbose_name='评论内容', null=False)
    parent = models.ForeignKey("self", on_delete=models.SET_NULL, related_name="replies", null=True, blank=True,
                               verbose_name='父评论')
    rating = models.SmallIntegerField(verbose_name='评分', choices=RATING_CHOICES, default=RATING_CHOICES[0][0])
    teacher = models.ForeignKey(Teacher, on_delete=models.SET_NULL, related_name='comments', verbose_name='评论的老师',
                                null=True, blank=True, default=None)
    article = models.ForeignKey(Article, on_delete=models.SET_NULL, related_name='comments', verbose_name='评论的文章',
                                null=True, blank=True, default=None)
    like_users = models.ManyToManyField(User, related_name='like_comments', verbose_name='喜欢该评论的用户')

    @property
    def like_count(self):
        return self.like_users.count()

    @property
    def replies_count(self):
        return self.replies.count()

    class Meta:
        db_table = "tb_comments"
        verbose_name = "评论"
        verbose_name_plural = "评论"
